#!/usr/bin/env python3

# -*- coding: utf-8 -*-

# @author: zhangbo

# @time: 2025/3/14 19:52

import pytest
from typing import List

from selenium.webdriver.common.by import By

from utils.read_yaml import Read_yaml
from utils.tools import get_project_name, sep
from common.driver_config import DriverConfig
from page.LoginPage import LoginPage
from common.ObjectMap import ObjectMap


@pytest.fixture()
def login_steps():
    """
    用例的登录前置步骤
    :return:
    """

    # yaml文件路径
    yaml_path: List[str] = [sep([get_project_name(), r"config\login_config.yaml"])]

    # 读取用例所需参数
    config = Read_yaml(yaml_path).get_config_data("login_config")

    # 启动浏览器
    driver = DriverConfig().edge_Driver_Config()

    # 获取访问链接
    driver.get(config["login_config"]["url"])

    login_page = LoginPage(yaml_path)

    # 输入账号密码
    login_page.login_account_input_value(driver)
    login_page.login_password_input_value(driver)

    # 点击登录
    login_page.login_button_click(driver)

    try:
        # 等待登录按钮消失
        ObjectMap().wait_element_displayed(driver=driver, location_type=By.XPATH,
                                           location_value=config["login_config"]["login_button"])

        # 等待首页账号div 出现
        ObjectMap().wait_element_appear(driver=driver, location_type=By.XPATH,
                                        location_value=config["login_config"]["loading_success"])

        # 等待页面加载完成
        ObjectMap().wait_for_ready_state_complete(driver=driver)
    except Exception as e:
        print("首页进入失败，失败原因：%s" % e)

    return driver
